package com.xlx.ui.domain.VO;

import com.alibaba.fastjson.JSONObject;

import com.xlx.ui.service.error.BaseErrorInfoInterface;
import com.xlx.ui.service.error.errorImpl.ResponseCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Description:统一数据返回格式
 * @author: 张川
 * @date: 2020-06-04 10:43
 */
@ApiModel(value = "统一数据返回格式", description = "统一数据返回格式")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultBody<T> {
    @ApiModelProperty(value = "响应代码")
    private int code;

    @ApiModelProperty(value = "响应消息")
    private String msg;

    @ApiModelProperty(value = "响应结果")
    private T data;


    public ResultBody(BaseErrorInfoInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.msg = errorInfo.getResultMsg();
    }


    /**
     * 成功
     *
     * @return
     */
    public static ResultBody success() {
        return success(null);
    }

    /**
     * 成功
     *
     * @param data
     * @return
     */
    public static ResultBody success(Object data) {
        ResultBody rb = new ResultBody();
        rb.setCode(ResponseCodeEnum.SUCCESS.getResultCode());
        rb.setMsg(ResponseCodeEnum.SUCCESS.getResultMsg());
        rb.setData(data);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error() {
        ResultBody rb = new ResultBody();
        rb.setCode(ResponseCodeEnum.INTERNAL_SERVER_ERROR.getResultCode());
        rb.setMsg(ResponseCodeEnum.INTERNAL_SERVER_ERROR.getResultMsg());
        return rb;
    }
    /**
     * 失败
     */
    public static ResultBody error(Object data) {
        ResultBody rb = new ResultBody();
        rb.setCode(ResponseCodeEnum.INTERNAL_SERVER_ERROR.getResultCode());
        rb.setMsg(ResponseCodeEnum.INTERNAL_SERVER_ERROR.getResultMsg());
        rb.setData(data);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error(BaseErrorInfoInterface errorInfo) {
        ResultBody rb = new ResultBody();
        rb.setCode(errorInfo.getResultCode());
        rb.setMsg(errorInfo.getResultMsg());
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error(int code, String message) {
        ResultBody rb = new ResultBody();
        rb.setCode(code);
        rb.setMsg(message);
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     */
    public static ResultBody error(BaseErrorInfoInterface errorInfo, Object data) {
        ResultBody rb = new ResultBody();
        rb.setCode(errorInfo.getResultCode());
        rb.setMsg(errorInfo.getResultMsg());
        rb.setData(data);
        return rb;
    }


    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

}
